﻿using System.IO.IsolatedStorage;
namespace Bango.WindowsPhone8.SDK
{
	public class BangoStorage
	{
		private static IsolatedStorageSettings _isolatedStore = IsolatedStorageSettings.ApplicationSettings;
		private static System.String _keyName = "BangoStore";
		private bool _useOfflineStorage;
		private BangoViewModel _bangoViewModel;
		private string _applicationId;

		public System.Boolean IsInitialised { get; set; }

		public System.Collections.Generic.List<StoredProfile> StoredProfiles()
		{
			return _bangoViewModel.StoredProfiles(_applicationId);            
		}

		#region Initialising

		internal BangoStorage(System.String applicationId, System.Boolean useOfflineStorage)
		{
			_useOfflineStorage = useOfflineStorage;
			_applicationId = applicationId;
			InitStorage();
		}

		internal void InitStorage()
		{
			if (!IsInitialised)
			{
				// Create the database if it does not exist.
				using (BangoDataContext db = new BangoDataContext())
				{
					if (db.DatabaseExists() == false)
					{
						// Create the local database.
						db.CreateDatabase();

						// Save changes to the database.
						db.SubmitChanges();
					}
				}
				
				IsInitialised = true;
			}
			_bangoViewModel = new BangoViewModel();
		}

		#endregion

		#region Settings

		public static System.Boolean HasSettings()
		{
			return (_isolatedStore.Keys.Count != 0);
		}

		public static System.Boolean UpdateSetting<T>(System.String keyName, T keyValue)
		{
			System.Boolean returnValue = false;
			System.String combinedKeyName = _keyName + "_" + keyName;
			try
			{
				if (_isolatedStore.Contains(combinedKeyName))
				{
					_isolatedStore[combinedKeyName] = keyValue;
				}
				else
				{
					_isolatedStore.Add(combinedKeyName, keyValue);
				}
				_isolatedStore.Save();
				returnValue = true;
			}
			catch (System.Exception)
			{
				returnValue = false;
			}
			return returnValue;
		}

		public static T LoadSetting<T>(System.String keyName, T defaultValue)
		{
			T returnValue = defaultValue;
			System.String combinedKeyName = _keyName + "_" + keyName;
			try
			{
				if (_isolatedStore.Contains(combinedKeyName))
				{
					try
					{
						_isolatedStore.TryGetValue<T>(combinedKeyName, out returnValue);
					}
					catch (System.Exception)
					{
						returnValue = defaultValue;
					}
				}
				else
				{
					returnValue = defaultValue;
				}
			}
			catch (System.Exception)
			{

			}
			return returnValue;
		}

		#endregion

		
		internal void SaveChangesToDB()
		{
			_bangoViewModel.SaveChanges();
		}

		public int SaveProfile(BangoProfile bangoProfile)
		{
			return _bangoViewModel.SaveProfile(bangoProfile);
		}
		
		internal System.Boolean DeleteProfile(StoredProfile profile)
		{
			System.Boolean returnValue = false;
			try
			{
				returnValue = _bangoViewModel.DeleteProfile(profile);				
			}
			catch (System.Exception)
			{

			}

			return returnValue;
		}
	}
}
